/**
 * 测试组件，用于测试MyDialog组件，可以删除
 */
import React, { Component } from 'react';
import TextField from '@material-ui/core/TextField';
import Grid from '@material-ui/core/Grid';
import AddIcon from '@material-ui/icons/Add';
import Button from '@material-ui/core/Button';
import MyDialog from './MyDialog';

/**
 * 成绩系数输入
 */
class WeightInputGroup extends Component {
    constructor(props) {
        super(props);
        this.query = this.props.location.query;//传入的参数
        //测试数据,防止每次刷新都无数据导致报错
        if (!this.query) {
            this.query = {
                "_id": "5f4e1da702e91822e86f38d8",
                "courseid": "5.1",
                "coursenumber": "5",
                "coursekind": "必修课",
                "coursename": "软件设计与体系结构",
                "teachernumber": "5",
                "assessmentmethod": "考试",
                "classlist": [
                    "2017240206",
                    "2017240207"
                ],
                "term": "2020-2021第一学期",
                "status": 0,
                "gradepecents": {
                    '考试成绩': 0.7,
                    '平时成绩': 0.3,
                }
            }
        }
        this.state = {
            gradepecents: this.query.gradepecents || {},//成绩系数
            addDialog: {
                title:'新增成绩系数',
                prompt:'请填写新增的成绩系数的名称。',//提示信息
                textFields:[{
                    label:'成绩系数名',
                    onChange:this.handleChange
                }],//一个对象数组，键：label,onChange
                buttons:[{
                    text:'取消',
                    onClick:this.closeAddDialog
                },{
                    text:'添加',
                    onClick:(e)=>this.closeAddDialog(e,'add')
                }],//一个对象数组,键：text,onClick
                onClose:this.closeAddDialog,//关闭对话框的回调函数
                open:false,//对话框的开启状态
            },//用于输入成绩系数名称的对话框
            
            newWeightName:'',//新系数的名字
        }
        this.addDialogRef=React.createRef();

    }
    handleChange=(event)=>{
        this.setState({
            newWeightName:event.target.value
        });
    }



    /**
     * 添加成绩系数
     * @param {成绩系数名} weightName 
     */
    addWeight = (weightName) => {
        let newWeight = {
            [weightName]: 0
        };

        this.setState((state) => {
            return {gradepecents: Object.assign(newWeight, state.gradepecents)};
        })
    }

    /**
     * 打开添加对话框
     * @param {} event 
     */
    openAddDialog = (event) => {
        this.setState({
            addDialog:true
        })
    }

    /**
     * 关闭添加对话框
     * @param {} event 
     * @param {附加操作} op 
     */
    closeAddDialog = (event,op) => {
        this.setState({
            addDialog:false
        })
        if(op==='add'){
            //添加成绩系数
            this.addWeight(this.state.newWeightName);
        }
    }


    render() {
        return (
            <Grid container>
                {Object.keys(this.state.gradepecents).map(
                    (weightName, index) => {
                        return (
                            <Grid item xs={12}>
                                <TextField key={weightName} id="standard-basic" label={weightName} value={this.state.gradepecents[weightName]} />
                            </Grid>
                        );
                    }


                )}
                <Grid item xs={12}>
                    <Button
                        variant="contained"
                        color="primary"
                        endIcon={<AddIcon>send</AddIcon>}
                        onClick={(e)=>this.openAddDialog(e)}
                    >
                        添加
                    </Button>
                </Grid>
                {/**对话框 */}
                {/*}
                <Dialog open={this.state.addDialog} onClose={this.closeAddDialog} aria-labelledby="form-dialog-title">
                    <DialogTitle id="form-dialog-title">新增成绩系数</DialogTitle>
                    <DialogContent>
                        <DialogContentText>
                            请填写新增的成绩系数的名称。  
                        </DialogContentText>
                        <TextField
                            autoFocus
                            margin="dense"
                            id="name"
                            label="成绩系数名"
                            type="text"
                            fullWidth
                            onChange={this.handleChange}
                        />
                    </DialogContent>
                    <DialogActions>
                        <Button onClick={this.closeAddDialog} color="primary">
                            取消
                        </Button>
                        <Button onClick={(e)=>this.closeAddDialog(e,'add')} color="primary">
                            添加
                        </Button>
                    </DialogActions>
                </Dialog>
                */}
                <MyDialog
                    title={this.state.addDialog.title}
                    prompt={this.state.addDialog.prompt}
                    textFields={this.state.addDialog.textFields}
                    buttons={this.state.addDialog.buttons}
                    onClose={this.state.addDialog.onClose}
                    open={this.state.addDialog.open}
                    ref={this.addDialogRef}
                />


            </Grid>
        );
    }
}

export default WeightInputGroup;